package org.kjk.mail;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class GMailLoginController extends AbstractController {
	private GMail gmail;
	public List<GMailDTO> mail = null;

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("Login Call!");
		String id = request.getParameter("id");
		String pass = request.getParameter("pass");
		request.getSession().setAttribute("id", id);
		request.getSession().setAttribute("pass", pass);
		response.sendRedirect("readMail.do");

		if (id == null || id.length() == 0 || pass == null
				|| pass.length() == 0) {
			return new ModelAndView("/mail/gmailLogin.jsp");
		}
		gmail.setAccountDetails(id, pass);
		System.out.println("Page Loading");
		mail = gmail.getSubject();
		if (mail == null) {
			System.out.println("login fail!!");
			return new ModelAndView("mail/gmailLogin.jsp");
		}
		request.getSession().setAttribute("mail", mail);
		System.out.println("Login and page load success");
		return new ModelAndView("mail/mail.jsp", "mail", mail);
	}

	public void setGmail(GMail gmail) {
		this.gmail = gmail;
	}

}
